package binderipcremoteviews

import android.os.IBinder
import android.os.Parcel
import android.widget.RemoteViews

class TranscationRemoteViewsProxy(remote: IBinder) : ITranscationRemoteViews {

    private val mRemote = remote

    override fun addRemoteViews(): RemoteViews? {
        val data = Parcel.obtain()
        val reply = Parcel.obtain()
        try {
            data.writeInterfaceToken(ITranscationRemoteViews.TRANSCATION_REMOTE_VIEWS_DESCRIPTION)
            data.writeNoException()
            mRemote.transact(ITranscationRemoteViews.TRANSCATION_ADD_REMOTE_VIEWS, data, reply, 0)
            reply.readException()
            return reply.readValue(javaClass.classLoader) as? RemoteViews
        } finally {
            reply.recycle()
            data.recycle()
        }
    }

    override fun asBinder(): IBinder {
        return mRemote
    }
}